package com.gp.dwt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gp.dwt.base.Result;
import com.gp.dwt.dto.UserApplyDto;
import com.gp.dwt.mapper.RoleMapper;
import com.gp.dwt.mapper.UserMapper;
import com.gp.dwt.mapper.UserRoleApplyMapper;
import com.gp.dwt.service.RoleService;
import com.gp.entity.Role;
import com.gp.entity.User;
import com.gp.entity.UserRoleApply;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 小杜
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private UserRoleApplyMapper userRoleApplyMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public Result userApplyRole(UserRoleApply userRoleApply) {
        userRoleApplyMapper.insert(userRoleApply);
        return Result.responseResult();
    }

    @Override
    public Result userApplyList() {
        return Result.responseResult(userRoleApplyMapper.getApplyList());
    }

    @Override
    public Result addRole(Role role) {
        roleMapper.insert(role);
        return Result.responseResult();
    }

    @Override
    public Result auditApply(UserApplyDto userApplyDto) {
        String state=userApplyDto.getState();
        //处理审核通过的申请
        if("1".equals(state)) {
            Long userId = userApplyDto.getUserId();
            User user = userMapper.selectById(userId);
            user.setRoleid(userApplyDto.getApplyRoleId());
            userMapper.updateById(user);
        }
        UserRoleApply userRoleApply=userRoleApplyMapper.selectById(userApplyDto.getApplyId());
        userRoleApply.setState(userApplyDto.getState());
        userRoleApplyMapper.updateById(userRoleApply);
        return Result.responseResult();
    }
}
